<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">

      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list } from '@/api/equipmentManage/equipmentReport/equipmentPowerReport'
import { realStatusSelectList as getDeviceOptions } from '@/api/superManage/iotManage/iotConfiguration'
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  computed: {
    ...mapGetters([
      'companyId'
    ])
  },
  data() {
    return {
      loading: true,
      pageId: '设备电量统计', // 页面唯一标识
      pageExportId: '设备电量统计导出',
      pageData: {
        deviceId: '',
        deviceName: '',
        createTime: '',
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'deviceName', label: '设备信息', canOrder: false },
        { prop: 'deviceId', label: 'IOT编码' },
        { prop: 'power', label: '功率（kw）', canOrder: false },
        { prop: 'electricity', label: '电量（kwh）', canOrder: false },
        { prop: 'createTime', label: '接收时间' }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'deviceName', label: '设备信息', canOrder: false },
        { prop: 'deviceId', label: 'IOT编码' },
        { prop: 'power', label: '功率（kw）', canOrder: false },
        { prop: 'electricity', label: '电量（kwh）', canOrder: false },
        { prop: 'createTime', label: '接收时间' }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'deviceId', label: '设备信息', type: 'select', optionsName: 'deviceOptions' },
        // { prop: 'iotCode', label: 'IOT编码', type: 'input' },
        { prop: 'searchTime', label: '接收时间', type: 'time', isTime: true, hasDefault: true, defaultKey: 'defaultSearchTime' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '批量导出3', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      deviceOptions: [],
      defaultSearchTime: []
    }
  },
  mounted() {
    this.pageData.companyId = this.companyId
    this.getDeviceOptions()
    this.defaultSearchTime = [this.$moment(new Date()).format('YYYY-MM-DD') + ' 00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23']
  },
  methods: {
    getDeviceOptions() {
      getDeviceOptions({
        isAll: 0,
        iotType: '电表',
        pageNum: 1,
        pageSize: 9999,
        orderByColumn: 'updateTime',
        companyId: this.companyId,
        isAsc: 'desc'
      }).then(res => {
        res.data.forEach(item => {
          item.value = item.deviceId
          item.label = item.deviceName
        })
        this.pageData.deviceId = res.data[0].deviceId
        this.deviceOptions = res.data
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getExportData(data) {
      return list({ ...this.pageData, ...data })
    },
    search() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh() {
      // 清空搜索栏
      this.pageData.pageNum = 1
      this.pageData.deviceId = ''
      this.pageData.deviceName = ''
      this.pageData.createTime = ''
      this.search()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
